package com.integralPerformance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.integralPerformance.common.Result;
import com.integralPerformance.mapper.ApplicationRecodesMapper;
import com.integralPerformance.pojo.entity.ApplicationRecodes;
import com.integralPerformance.pojo.entity.PositionElement;
import com.integralPerformance.pojo.form.add.AddApplicationRecode;
import com.integralPerformance.pojo.form.update.UpdateApplicationRecode;
import com.integralPerformance.pojo.query.ApplicationRecodeQuery;
import com.integralPerformance.pojo.query.ApplicationRecodeTeacherQuery;
import com.integralPerformance.pojo.vo.ApplicationRecodesVo;
import com.integralPerformance.pojo.vo.BaseVo;
import com.integralPerformance.service.ApplicationRecodesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 * @description 针对表【application_recodes(岗位申请管理表)】的数据库操作Service实现
 * @createDate 2024-04-03 21:42:22
 */
@Service
public class ApplicationRecodesServiceImpl extends ServiceImpl<ApplicationRecodesMapper, ApplicationRecodes>
        implements ApplicationRecodesService {

    @Autowired
    ApplicationRecodesMapper applicationRecodesMapper;

    @Override
    public Result<BaseVo<ApplicationRecodesVo>> getList(ApplicationRecodeQuery applicationRecodeQuery) {
        Page<ApplicationRecodesVo> page = new Page<>(applicationRecodeQuery.getCurrentPage(), applicationRecodeQuery.getPageSize());
        Page<ApplicationRecodesVo> result = applicationRecodesMapper.getList(page, applicationRecodeQuery);

        //json 转成 List<PositionElement> 并且重新塞进去
        result.getRecords().forEach(
                (applicationManagerVo) -> {
                    Object poc = applicationManagerVo.getPerformanceOfContribution();
                    String string = (poc != null && !poc.toString().isEmpty()) ? poc.toString() : "[]";
                    List<PositionElement> positionElements = JSONArray.parseArray(string, PositionElement.class);
                    applicationManagerVo.setPerformanceOfContribution(positionElements);
                    System.out.println(positionElements);
                }
        );
        BaseVo<ApplicationRecodesVo> criteriaBaseVo = new BaseVo<>();
        criteriaBaseVo.setCurrentPage(applicationRecodeQuery.getCurrentPage());
        criteriaBaseVo.setPageSize(applicationRecodeQuery.getPageSize());
        criteriaBaseVo.setAllPages(result.getPages());
        criteriaBaseVo.setAllRecordNum(result.getTotal());
        criteriaBaseVo.setRecords(result.getRecords());

        return Result.success(criteriaBaseVo);
    }

    @Override
    public Result<String> addOne(AddApplicationRecode addApplicationRecode) {
        ApplicationRecodes applicationRecodes = new ApplicationRecodes();
        QueryWrapper<ApplicationRecodes> queryWrapper = new QueryWrapper<>();

        Map<String,Object> map = new HashMap<>();
        map.put("year",addApplicationRecode.getYear());
        map.put("teacher_id",addApplicationRecode.getTeacherId());
        map.put("type_name",addApplicationRecode.getTypeName());
        map.put("level_name",addApplicationRecode.getLevelName());
        map.put("corresponding_name",addApplicationRecode.getCorrespondingName());

        queryWrapper.allEq(map);

        Long count = applicationRecodesMapper.selectCount(queryWrapper);
        if (count>0){
            return Result.fail("您已申请，请勿重复申请,请到申请记录中查看岗位申请情况");
        }
        BeanUtil.copyProperties(addApplicationRecode,applicationRecodes);
        List<PositionElement> poc = addApplicationRecode.getPerformanceOfContribution();
        String jsonString;
        if (poc != null) {
            jsonString = JSON.toJSONString(poc);
        } else {
            jsonString = "[]";
        }
        applicationRecodes.setPerformanceOfContribution(jsonString);
        applicationRecodesMapper.insert(applicationRecodes);
        return Result.success("添加成功");
    }

    @Override
    public Result<String> updateOne(UpdateApplicationRecode updateApplicationRecode) {

        Integer id = updateApplicationRecode.getId();
        ApplicationRecodes applicationRecodes = applicationRecodesMapper.selectById(id);

        if (!applicationRecodes.getTeacherId().equals(updateApplicationRecode.getTeacherId())){
            return  Result.fail("请勿恶意修改他人数据");
        }
        if (applicationRecodes.getReviewStatus().equals("通过")){
            return Result.fail("修改失败，请联系管理员退回");
        }
        ApplicationRecodes recodes = new ApplicationRecodes();
        BeanUtil.copyProperties(updateApplicationRecode,recodes);
        //重新修改状态为待审核
        recodes.setReviewStatus("待审核");
        List<PositionElement> poc = updateApplicationRecode.getPerformanceOfContribution();
        String jsonString;
        if (poc != null) {
            jsonString = JSON.toJSONString(poc);
        } else {
            jsonString = "[]";
        }
        recodes.setPerformanceOfContribution(jsonString);
        applicationRecodesMapper.updateById(recodes);

        return Result.success("修改成功");
    }

    @Override
    public Result<BaseVo<ApplicationRecodesVo>> getByTeacherId(ApplicationRecodeTeacherQuery applicationRecodeTeacherQuery, String teacherId) {
        Page<ApplicationRecodesVo> page = new Page<>(applicationRecodeTeacherQuery.getCurrentPage(), applicationRecodeTeacherQuery.getPageSize());
        Page<ApplicationRecodesVo> result = applicationRecodesMapper.getByTeacherId(page, applicationRecodeTeacherQuery,teacherId);

        //json 转成 List<PositionElement> 并且重新塞进去
        result.getRecords().forEach(
                (applicationManagerVo) -> {
                    Object poc = applicationManagerVo.getPerformanceOfContribution();
                    String string = (poc != null && !poc.toString().isEmpty()) ? poc.toString() : "[]";
                    List<PositionElement> positionElements = JSONArray.parseArray(string, PositionElement.class);
                    applicationManagerVo.setPerformanceOfContribution(positionElements);
                    System.out.println(positionElements);
                }
        );

        BaseVo<ApplicationRecodesVo> criteriaBaseVo = new BaseVo<>();
        criteriaBaseVo.setCurrentPage(applicationRecodeTeacherQuery.getCurrentPage());
        criteriaBaseVo.setPageSize(applicationRecodeTeacherQuery.getPageSize());
        criteriaBaseVo.setAllPages(result.getPages());
        criteriaBaseVo.setAllRecordNum(result.getTotal());
        criteriaBaseVo.setRecords(result.getRecords());

        return Result.success(criteriaBaseVo);
    }

    @Override
    public Result<String> deleteById(String id, String teacherId) {
        ApplicationRecodes applicationRecodes = applicationRecodesMapper.selectById(id);
        if (!applicationRecodes.getTeacherId().equals(teacherId)){
            return Result.fail("请勿恶意删除他人数据");
        }
        if (applicationRecodes.getReviewStatus().equals("通过")){
            return  Result.fail("删除失败，请联系管理员退回，仅有\"退回\"、\"待审核\"状态的允许被删除");
        }
        applicationRecodesMapper.deleteById(id);

        return Result.success("删除成功");
    }

    @Transactional
    @Override
    public Result<String> agreeByIds(List<Integer> list) {
        applicationRecodesMapper.agreeByIds(list);
        return Result.success("审核成功");
    }

    @Transactional
    @Override
    public Result<String> disagreeByIds(List<Integer> list) {
        applicationRecodesMapper.disagreeByIds(list);
        return Result.success("退回成功");
    }

    @Override
    public Result<?> countStatus() {
        return Result.success("等待开发ing");
    }

}




